﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GTA
{
    public partial class FrmChangeRequestResults : BaseForm
    {
        #region Local objects
        private List<BusinessEntity.TaskDomain> changeRequestResults;
        private BusinessEntity.TaskDomain changeRequestResultEdit;
        #endregion

        #region Starting Form
        public FrmChangeRequestResults()
        {
            InitializeComponent();
        }
        private void FrmChangeRequestResults_Load(object sender, EventArgs e)
        {
            StartForm();
        }
        private void StartForm()
        {
            LoadCurrentResults();
        }
        #endregion

        #region Events
        private void cmdSave_Click(object sender, EventArgs e)
        {
            SaveChangeRequestResult();
        }
        private void cmdEdit_Click(object sender, EventArgs e)
        {
            EditSelectedChangeRequestResult();
        }
        private void cmdDelete_Click(object sender, EventArgs e)
        {
            DeleteSelectedChangeResult();
        }
        private void editarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditSelectedChangeRequestResult();
        }
        private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DeleteSelectedChangeResult();
        }
        private void lstResults_DoubleClick(object sender, EventArgs e)
        {
            EditSelectedChangeRequestResult();
        }
        private void lstResults_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            SortList(lstResults, GetLstResultsColumnType(e.Column), ref columnOrder, ref sortOrder, e.Column);
        }
        private void lstResults_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
                DeleteSelectedChangeResult();
        }
        private void lstResults_Resize(object sender, EventArgs e)
        {
            lstResults.Columns[1].Width = lstResults.Width - lstResults.Columns[0].Width - 20;
        }
        #endregion

        #region Methods
        private OrdenacaoListView.Tipo GetLstResultsColumnType(int column)
        {
            switch (column)
            {
                case 0:
                    return OrdenacaoListView.Tipo.Numerica;
                case 1:
                    return OrdenacaoListView.Tipo.Alfabetica;
                default:
                    return OrdenacaoListView.Tipo.Alfabetica;
            }
        }
        private void LoadCurrentResults()
        {
            try
            {
                BusinessLogic.GTA bl = new BusinessLogic.GTA();
                changeRequestResults = bl.ListChangeRequestResults();
                lstResults.Items.Clear();
                if (changeRequestResults != null && changeRequestResults.Count > 0)
                {
                    foreach (BusinessEntity.TaskDomain itemResult in changeRequestResults)
                    {
                        ListViewItem listItem = new ListViewItem(itemResult.ID.ToString());
                        listItem.SubItems.Add(itemResult.Description);
                        lstResults.Items.Add(listItem);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Carregar a lista de Change Request Results existentes.", ex);
            }
        }
        private void SaveChangeRequestResult()
        {
            try
            {
                if (!String.IsNullOrEmpty(txtDescription.Text.Trim()))
                {
                    BusinessEntity.TaskDomain changeRequest = new BusinessEntity.TaskDomain(0, txtDescription.Text);
                    if (changeRequestResultEdit != null)
                        changeRequest.ID = changeRequestResultEdit.ID;
                    BusinessLogic.GTA bl = new BusinessLogic.GTA();
                    bl.SaveChangeRequestResult(changeRequest);
                    MessageBox.Show("Change Request Result salva com sucesso!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtDescription.Text = String.Empty;
                    txtDescription.Focus();
                    changeRequestResultEdit = null;
                    LoadCurrentResults();
                }
                else
                    MessageBox.Show("Informe a descrição!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                HandleException("Salvar os dados da Change Request Result.", ex);
            }
        }
        private void EditSelectedChangeRequestResult()
        {
            try
            {
                if (lstResults.SelectedItems.Count == 1)
                {
                    changeRequestResultEdit = changeRequestResults.Find(it =>
                        it.ID.CompareTo(Convert.ToInt32(lstResults.SelectedItems[0].SubItems[0].Text)) == 0);
                    txtDescription.Text = changeRequestResultEdit.Description;
                    txtDescription.Focus();
                }
                else
                    MessageBox.Show("Selecione uma Change Request Result para editar!", "Alerta", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            catch (Exception ex)
            {
                HandleException("Editar a Change Request Result.", ex);
            }
        }
        private void DeleteSelectedChangeResult()
        {
            try
            {
                if (lstResults.SelectedItems.Count > 0)
                {
                    if (MessageBox.Show("Deseja realmente excluir as Change Request Results selecionadas?", "Questão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        BusinessLogic.GTA bl = new BusinessLogic.GTA();
                        foreach (ListViewItem listItem in lstResults.SelectedItems)
                            bl.DeleteChangeRequestResult(Convert.ToInt32(listItem.SubItems[0].Text));
                        MessageBox.Show("Change Request Results excluídos com sucesso!", "Confirmação", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadCurrentResults();
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException("Excluir as Change Request Results selecionadas.", ex);
            }
        }
        #endregion
    }
}
